package online.westonline.com.westonline.base;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.MenuItem;

import java.util.Map;

/**
 * 最最最上面的fragment
 * Created by admin on 2018/4/16.
 */

public class LibFragment extends Fragment
{
    private ProgressDialog dialog;
    private AlertDialog.Builder builder;

    /**
     * 打开进度框
     * @param msg
     */
    protected void showProgessDialog(String msg)
    {
        if (!TextUtils.isEmpty(msg))
        {
            dialog = new ProgressDialog(getActivity());
            dialog.setMessage(msg);
            dialog.show();
        }
    }
    /**
     * 打开普通对话框
     * @param title 标题
     * @param msg 内容
     * @param flag 是否强制关闭
     * @param onClickListener 监听
     */
    protected void showNormalDialog(boolean flag,String title, String msg,String cancle, DialogInterface.OnClickListener onClickListener)
    {
        builder = new AlertDialog.Builder(getActivity());
        builder.setTitle(title);
        builder.setCancelable(flag);
        builder.setMessage(msg);
        builder.setPositiveButton("确定",onClickListener);
        builder.setNegativeButton(cancle, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //空实现达到关闭对话框的目的
            }
        });
        builder.show();
    }
    /**
     * 延时关闭进度框
     * @param msg
     * @param mil
     */
    protected void showProgressDialog(String msg,long mil)
    {
        dialog = new ProgressDialog(getActivity());
        dialog.setCancelable(false);
        dialog.setMessage(msg);
        dialog.show();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                dialog.hide();
            }
        },mil);
    }

    /**
     * 关闭
     */
    protected void dissmissDialog()
    {
        if (dialog!=null)
        {
            dialog.hide();
        }
    }

    /**
     * 跳转页面
     * @param clazz
     * @param flag
     */
    protected void startActivity(Class clazz,boolean flag)
    {
        if (flag)
        {
            startActivity(new Intent(getActivity(),clazz));
            getActivity().finish();
            return;
        }
        startActivity(new Intent(getActivity(),clazz));
    }

    /**
     * 带数据跳转
     * @param clazz
     * @param flag
     * @param bundle
     */
    protected void startActivityWithBundle(Class clazz, boolean flag, Bundle bundle)
    {
        Intent intent = new Intent(getActivity(),clazz);
        intent.putExtras(bundle);
        if (flag)
        {
            startActivity(intent);
            getActivity().finish();
            return;
        }
        startActivity(intent);
    }

    /***
     * 带数据
     * @param clazz
     * @param flag
     * @param params
     */
    protected void startActivityWithParams(Class clazz, boolean flag, Map<String,String> params)
    {
        Intent intent = new Intent(getActivity(),clazz);
        for (String key:params.keySet())
        {
            intent.putExtra(key,params.get(key));
        }
        if (flag)
        {
            startActivity(intent);
            getActivity().finish();
            return;
        }
        startActivity(intent);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId())
        {
            case android.R.id.home:
                getActivity().finish();
                break;
        }
        return true;
    }
}
